import * as mars3d from "mars3d";
import * as Cesium from "mars3d-cesium";

// import * as turf from "@turf/turf";

// import WKT from "terraformer-wkt-parser";

class mapUtil {
  mapInstance;
  // 测量工具图层
  measureLayer
  constructor (map) {
    if (map) this.mapInstance = map;
    else throw new Error("constructor参数不能为空！");
    // 初始化测量工具图层并加入图层组
    this.measureLayer = new mars3d.thing.Measure({
      id: "measure",
      label: {
        color: "#ffffff",
        font_family: "楷体",
        font_size: 20,
        background: false,
      },
      isAutoEditing: false // 关闭编辑功能（放置卡顿）
    });
    this.mapInstance.addThing(this.measureLayer);
    
  }

  // 量算工具
  measureHandle(command) {
    switch (command) {
      case "clear":
        this.measureLayer.clear();
        break;
      case "空间距离":
        this.measureLayer.distance({
          showAddText: true,
        });
        break;
      case "贴地距离":
        this.measureLayer.distanceSurface({
          showAddText: true,
          exact: false, // 是否进行精确计算， 传false时是否快速概略计算方式，该方式计算精度较低，但计算速度快，仅能计算在当前视域内坐标的高度
        });
        break;
      case "水平面积":
        this.measureLayer.area().then(async (graphic) => {
          const oldPositions = graphic.positionsShow;
          const rang = await mars3d.PolyUtil.getHeightRangeByDepth(
            oldPositions,
            this.mapInstance.scene
          );
          graphic.positions = mars3d.PointUtil.setPositionsHeight(
            oldPositions,
            rang.maxHeight
          );
        });
        break;
      case "贴地面积":
        this.measureLayer.areaSurface({
          style: {
            color: "#ffff00",
          },
          splitNum: 10, // step插值分割的个数
          exact: false, // 是否进行精确计算， 传false时是否快速概略计算方式，该方式计算精度较低，但计算速度快，仅能计算在当前视域内坐标的高度
        });
        break;
      case "高度差测量":
        this.measureLayer.height();
        break;
      case "三角测量":
        this.measureLayer.heightTriangle();
        break;
      case "方位角测量":
        this.measureLayer.angle();
        break;
      case "坐标测量":
        this.measureLayer.point();
        break;
      default:
        break;
    }
  }

}

export default mapUtil;
